<?php
namespace app\admin\controller;
use think\Db;
use think\Controller;
class Login extends Controller
{
    public function index()
    {          	
        if (request()->isPost()) {
            $data = input('post.');
            $user = Db::name('userAdmin')->where('open', 1)->where('name', $data['name'])->find();
            $datas['ip'] = xss($_SERVER['REMOTE_ADDR']);
            $datas['name'] = xss($data['name']);
            $key = 'www.115cms.com';
            $datas['password'] = xss($data['password']);
            $datas['time'] = time();
            $d=Date_k(0);
            $x=Date_k(1);
            $ca= Db::name('user_admin_log')->where('ip',$_SERVER["REMOTE_ADDR"])->where('open',0)->where("time > $d and time < $x")->count();
            if ($ca>=cms('adminlogo')) {
                return json(array('code' => 0, 'msg' => '今日登录错误次数过多明天再来'));
            }
            if ($user) {
                if (jiami($user['password'], 'DECODE', $key, 0) == $datas['password']) {
                    session('admin_name', $user['name']);
                    session('admin_id', $user['id']);
                    $datas['open'] = 1;
                	Db::name('userAdminLog')->insert($datas);
                    return json(array('code' => 200, 'msg' => '登录成功'));
                } else {
                	$datas['open'] = 0;
                	Db::name('userAdminLog')->insert($datas);
                    return json(array('code' => 0, 'msg' => '密码错误'));
                }
            } else {
            	$datas['open'] = 0;
            	Db::name('userAdminLog')->insert($datas);
                return json(array('code' => 0, 'msg' => '账号错误'));
            }
        }
        return $this->fetch();
    }
    public function logout()
    {
        session("admin_id", NULL);
        session("admin_name", NULL);
        return json(array('code' => 200, 'msg' => '退出成功'));
        return NULL;
        xhw();
    }
}